package cn.com.blueInfo.bpm.template.service.impl;

import cn.com.blueInfo.bpm.template.convert.InstanceConvert;
import cn.com.blueInfo.bpm.template.entity.Instance;
import cn.com.blueInfo.bpm.template.repository.InstanceRepository;
import cn.com.blueInfo.bpm.template.request.InstanceDTO;
import cn.com.blueInfo.bpm.template.response.InstanceVO;
import cn.com.blueInfo.bpm.template.service.InstanceService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 流程运行时实例表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:30:44
 */
@Log4j2
@Service
public class InstanceServiceImpl implements InstanceService {

    @Autowired
    private InstanceRepository instanceRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private InstanceConvert instanceConvert;

    /**
     * 新增流程运行时实例
     * @param instanceDTO 流程运行时实例参数
     */
    @Override
    public ResultInfo<Integer> addInstance(InstanceDTO instanceDTO) {
        Instance instance = instanceConvert.dtoToEntity(instanceDTO);
        instance.setId(snowflakeIdGenerator.nextId());
        Integer addSize = instanceRepository.createInstance(instance);
        return ResultInfo.<Integer>builder().data(addSize).message("流程运行时实例成功新增 {} 条").build();
    }

    /**
     * 批量新增流程运行时实例
     * @param instanceDTOList 流程运行时实例参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddInstance(List<InstanceDTO> instanceDTOList) {
        List<Instance> instanceList = instanceConvert.dtoToEntity(instanceDTOList);
        for (Instance instance : instanceList) instance.setId(snowflakeIdGenerator.nextId());
        Integer addSize = instanceRepository.batchCreateInstance(instanceList);
        return ResultInfo.<Integer>builder().data(addSize).message("流程运行时实例成功新增 {} 条").build();
    }

    /**
     * 删除流程运行时实例
     * @param instanceId 流程运行时实例ID
     */
    @Override
    public ResultInfo<Integer> removeInstance(Long instanceId) {
        Integer removeSize = instanceRepository.deleteInstance(instanceId);
        return ResultInfo.<Integer>builder().data(removeSize).message("流程运行时实例成功删除 {} 条").build();
    }

    /**
     * 批量删除流程运行时实例
     * @param instanceIdList 流程运行时实例ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveInstance(List<Long> instanceIdList) {
        Integer removeSize = instanceRepository.batchDeleteInstance(instanceIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("流程运行时实例成功删除 {} 条").build();
    }

    /**
     * 更新部分流程运行时实例
     * @param instanceUpdatePartialDTO 流程运行时实例参数
     */
    @Override
    public ResultInfo<Integer> modifyInstancePartial(InstanceDTO.UpdatePartialDTO instanceUpdatePartialDTO) {
        Instance instance = instanceConvert.dtoToEntity(instanceUpdatePartialDTO);
        Integer modifySize = instanceRepository.updateInstancePartial(instance);
        return ResultInfo.<Integer>builder().data(modifySize).message("流程运行时实例成功更新 {} 条").build();
    }

    /**
     * 更新全部流程运行时实例
     * @param instanceDTO 流程运行时实例参数
     */
    @Override
    public ResultInfo<Integer> modifyInstance(InstanceDTO instanceDTO) {
        Instance instance = instanceConvert.dtoToEntity(instanceDTO);
        Integer modifySize = instanceRepository.updateInstance(instance);
        return ResultInfo.<Integer>builder().data(modifySize).message("流程运行时实例成功更新 {} 条").build();
    }

    /**
     * 查询一条流程运行时实例
     * @param instanceId 流程运行时实例ID
     */
    @Override
    public ResultInfo<InstanceVO> queryInstanceById(Long instanceId) {
        Instance instance = instanceRepository.getInstanceById(instanceId);
        InstanceVO instanceVO = instanceConvert.entityToVO(instance);
        return ResultInfo.<InstanceVO>builder().data(instanceVO).message("流程运行时实例查询成功").build();
    }

    /**
     * 分页查询流程运行时实例
     * @param instanceQueryDTO 流程运行时实例参数
     */
    @Override
    public ResultInfo<Page<InstanceVO>> queryInstancePage(InstanceDTO.QueryDTO instanceQueryDTO) {
        Page<Instance> instancePage = instanceRepository.getInstancePage(instanceQueryDTO);
        Page<InstanceVO> resultData = instanceConvert.convertPage(instancePage);
        return ResultInfo.<Page<InstanceVO>>builder()
                .data(resultData)
                .message("流程运行时实例分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 流程运行时实例 数量
     * @param instanceQueryDTO 流程运行时实例参数
     */
    @Override
    public ResultInfo<Long> queryInstanceCount(InstanceDTO.QueryDTO instanceQueryDTO) {
        Long resultCount = instanceRepository.getInstanceCount(instanceQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("流程运行时实例数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 流程运行时实例
     * @param instanceQueryDTO 流程运行时实例参数
     */
    @Override
    public ResultInfo<List<InstanceVO>> queryInstanceList(InstanceDTO.QueryDTO instanceQueryDTO) {
        List<Instance> instanceList = instanceRepository.getInstanceList(instanceQueryDTO);
        List<InstanceVO> resultData = instanceConvert.entityListToVoList(instanceList);
        return ResultInfo.<List<InstanceVO>>builder()
                .data(resultData)
                .message("流程运行时实例分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
